from typing import Optional
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI
import os

# 定义 Pydantic 模型
class UserInfo(BaseModel):
    """Extracted user information, such as name, age, email, and phone number, if relevant."""
    name: str = Field(description="The name of the user")
    age: Optional[int] = Field(description="The age of the user")
    email: str = Field(description="The email address of the user")
    phone: Optional[str] = Field(description="The phone number of the user")
  
llm = ChatOpenAI(
  base_url='http://localhost:11434/v1',
  api_key=os.environ['DEEPSEEK_API_KEY'],
  model = 'qwen3:4b',
)
structured_llm = llm.with_structured_output(UserInfo)
res = structured_llm.invoke('我叫姚明，今年18岁，来自武汉，手机号是1231412，邮箱地址是1231412@qq.com')
print(res)